'''
Created on Dec 4, 2009

@author: Roni
'''

import graph
import cliques.utils

def create_index_graph(webgraph):
    ''' Create a graph that is isomorphic the web graph but with 
    nodes that are not host names but integers '''
    
    index_to_site = list()
    index_graph = graph.graph()
    
    index = 0
    for node in webgraph.nodes():
        index_to_site.append(node)
        index_graph.add_node(index)
        index = index+1
        
    for index in xrange(len(index_to_site)):
        node = index_to_site[index]
        for neighbor in webgraph.neighbors(node):
            neighbor_index = index_to_site.index(neighbor)
            index_graph.add_edge(index, neighbor_index)
    return (index_graph,index_to_site)

def write_webgraph_files(file_name,webgraph):
    ''' Outputs two files. The first file is a file that maps each 
    node in the web graph to an index. The second file contains 
    a graph that is isomorphic to the webgraph but uses indices instead
    of full host name for nodes '''
    (index_graph,index_to_site) = create_index_graph(webgraph)
    cliques.utils.export_graph(index_graph, file_name + ".graph", "dot")
    site_file = open(file_name+".sites","w")
    for index in xrange(len(index_to_site)):
        site_file.write("%d\t%s\n" % (index,index_to_site[index]))
    site_file.close()